package com.hejing.bpeldesign.bpelprase;

import com.hejing.bpeldesign.model.BpelNode;
import com.hejing.bpeldesign.model.ExpressionNode;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.UUID;

public class ExpressionNodeParser implements NodeParserStrategy {
    @Override
    public BpelNode parse(Element el) {
        String id = UUID.randomUUID().toString();
        String name = el.getAttribute("name");
        String bounds = el.getAttribute("bounds");
        String[] parts = bounds.split(",");
        int x = Integer.parseInt(parts[0]);
        int y = Integer.parseInt(parts[1]);
        int w = Integer.parseInt(parts[2]);
        int h = Integer.parseInt(parts[3]);

        ExpressionNode node = new ExpressionNode(id, name, el.getTagName(), x, y, w, h);
        NodeList funcs = el.getElementsByTagName("function");
        for (int i = 0; i < funcs.getLength(); i++) {
            node.addFunction(((Element) funcs.item(i)).getAttribute("value"));
        }
        return node;
    }
}